const express = require("express");
const db = require("../module/DBHeler");
const moment = require("moment");
const GoodsRouter = express.Router()


GoodsRouter.post("/addCart", async function (req, res) {

    try {
        //1.查询是否已经购买过  购买人和产品名称
        let query = [
            req.body.pname,
            req.body.uid
        ]
        //2.查询的sql语句
        let sql = "SELECT * FROM `cartes` WHERE `c_name`=? AND `u_id`=? ;"

        let query_result = await db.exec(sql, query);
        let isAdd = query_result && query_result.length >= 1;
        //isAdd是true,已经购买过
        if (isAdd) {

            let update_prams = [
                req.body.pnum,
                req.body.pname,
                req.body.uid,
            ]

            let update_sql = "UPDATE `cartes` SET   `c_status`=1,`c_num`=c_num+?,`c_total`=c_num*`c_price` WHERE `c_name`=? AND `u_id`=? ;"

            let update_result = await db.exec(update_sql, update_prams);
            let isUpdate = update_result && update_result.affectedRows >= 1;

            res.json({
                msg: isUpdate ? "加入购车成功!" : "加入失败成功!",
                code: isUpdate ? 200 : -200
            })


        } else {
            let insert_params = [
                req.body.pimg,
                req.body.pname,
                req.body.pspec,
                req.body.pprice,
                req.body.pnum,
                
                req.body.pprice * req.body.pnum,
                moment().format("YYYY-MM-DD hh:mm:ss"),
                req.body.uid,
                
                
            ]
            let insert_sql = "INSERT INTO `cartes`(`c_img`,`c_name`,`c_size`,`c_price`,`c_num`,`c_total`,`c_time`,`u_id`)";
            insert_sql += "VALUES(?,?,?,?,?,?,?,?) ;"

            let insert_result = await db.exec(insert_sql, insert_params);
            let isInsert = insert_result && insert_result.affectedRows >= 1;
            res.json({
                msg: isInsert ? "加入购车成功!" : "加入失败成功!",
                code: isInsert ? 200 : -200
            })
        }
    } catch (error) {
        res.json({
            msg: "加入购车失败!",
            code: -200,
            error
        })
    }

})


GoodsRouter.get("/search", async (req, res) => {
    let params = [
        req.query.uid  // 只看当前登录人的 购买信息
    ]
    let sql = "SELECT * FROM `cartes` WHERE `u_id`=? AND `c_status`=1;"

    try {
        let results = await db.exec(sql, params)

        res.json({
            msg: "查询成功",
            code: 200,
            data: results
        })
    } catch (error) {
        res.json({
            msg: "查询失败",
            code: -200,
            error
        })
    }

})


GoodsRouter.post("/update", async (req, res) => {
    let params = [
        req.body.pnum,
        req.body.cid
    ]
    let sql = "UPDATE `cartes` SET `c_num`=? WHERE `c_id`=? AND `c_status`=1 ;";

    try {

        let result = await db.exec(sql, params);
        let isUpdata = result && result.affectedRows >= 1;
        res.json({
            msg: isUpdata ? "修改成功" : "修改失败",
            code: isUpdata ? 200 : -200
        })
    } catch (error) {
        res.json({
            msg: "修改失败",
            code: -200,
            error
        })
    }

})


//删除
GoodsRouter.post("/delete", async (req, res) => {
    let params = [
        req.body.cid
    ]
    let sql = "UPDATE `cartes` SET `c_status`=0,`c_num`=0 WHERE `c_id`=? AND `c_status`=1;";

    try {
        let result = await db.exec(sql, params);
        let isDel = result && result.affectedRows >= 1;
        res.json({
            msg: isDel ? "删除成功" : "删除失败",
            code: isDel ? 200 : -200
        })
    } catch (error) {
        res.json({
            msg: "删除失败",
            code: -200,
            error
        })
    }

})

module.exports = {
    GoodsRouter
}